package com.biz.storage.config;

import io.minio.*;
import org.apache.commons.lang.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.context.annotation.*;

/**
 * @Description
 *      如果storage.service.type 不是 minio
 *      则不加载
 * @Author liuxi
 * @Date 2024/4/26
 **/
@Configuration
@ConditionalOnProperty(name = "storage.service.type", havingValue = "MINIO")
public class MinioConfig {

    @Value("${storage.minio.url}")
    private String url;

    @Value("${storage.minio.access-key}")
    private String accessKey;

    @Value("${storage.minio.secret-key}")
    private String secretKey;

    @Bean
    public MinioClient getMinioClient() {
        if (StringUtils.isBlank(this.url)) {
            throw new RuntimeException("[url] cannot be empty!!");
        }
        if (StringUtils.isBlank(this.accessKey)) {
            throw new RuntimeException("[accessKey] cannot be empty!!");
        }
        if (StringUtils.isBlank(this.secretKey)) {
            throw new RuntimeException("[secretKey] cannot be empty!!");
        }
        return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
    }
}
